package com.societyGames.catExercises
{
import com.societyGames.action.ActionQueue;
import com.societyGames.flashForms.HorizontalStackContainer;
import com.societyGames.flashForms.VerticalStackContainer;

import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;

public class CatRender extends Sprite
{
  [Embed(source="../../../../../CatExercises/assets/CatSit.png")]
  private static var _catSitClass:Class;

  [Embed(source="../../../../../CatExercises/assets/Hat.png")]
  private static var _hatClass:Class;

  [Embed(source="../../../../../CatExercises/assets/HatIcon.png")]
  private static var _hatIconClass:Class;

  [Embed(source="../../../../../CatExercises/assets/Mitten0.png")]
  private static var _mitten0Class:Class;

  [Embed(source="../../../../../CatExercises/assets/Mitten1.png")]
  private static var _mitten1Class:Class;

  [Embed(source="../../../../../CatExercises/assets/Mitten2.png")]
  private static var _mitten2Class:Class;

  [Embed(source="../../../../../CatExercises/assets/Mitten3.png")]
  private static var _mitten3Class:Class;

  [Embed(source="../../../../../CatExercises/assets/MittenIcon.png")]
  private static var _mittenIconClass:Class;

  [Embed(source="../../../../../CatExercises/assets/Bat.png")]
  private static var _batClass:Class;

  private var _catSimulation:CatSimulation;
  private var _actionQueue:ActionQueue;
  private var _cats:Vector.<DisplayObjectContainer> = new Vector.<DisplayObjectContainer>();
  private var _surplusHatDisplay:SurplusDisplay;
  private var _surplusMittenDisplay:SurplusDisplay;

  public function CatRender(catSimulation:CatSimulation, actionQueue:ActionQueue)
  {
    this._catSimulation = catSimulation;
    this._actionQueue = actionQueue;
  }

  public function initialize(catCount:int, batCount:int):void
  {
    var batsStackContainer:HorizontalStackContainer = new HorizontalStackContainer();
    for (var batIndex:int = 0; batIndex < batCount; batIndex++)
    {
      var batImage:DisplayObject = new CatRender._batClass();
      batsStackContainer.addItem(batImage);
    }

    var catsStackContainer:HorizontalStackContainer = new HorizontalStackContainer();
    for (var catIndex:int = 0; catIndex < catCount; catIndex++)
    {
      var catContainer:Sprite = new Sprite();
      catContainer.addChild(new CatRender._catSitClass());
      this._cats.push(catContainer);
      catsStackContainer.addItem(catContainer);
    }

    this._surplusHatDisplay = new SurplusDisplay(new _hatIconClass());
    this._surplusHatDisplay.count = 0;
    this._surplusHatDisplay.visible = false;

    this._surplusMittenDisplay = new SurplusDisplay(new _mittenIconClass());
    this._surplusMittenDisplay.count = 0;
    this._surplusMittenDisplay.visible = false;

    var surplusContainer:Sprite = new Sprite();
    surplusContainer.addChild(this._surplusHatDisplay);
    surplusContainer.addChild(this._surplusMittenDisplay);
    catsStackContainer.addItem(surplusContainer);

    var catsBatsContainer:VerticalStackContainer = new VerticalStackContainer();
    catsBatsContainer.addItem(batsStackContainer);
    catsBatsContainer.addItem(catsStackContainer);

    this.addChild(catsBatsContainer);
  }

  public function buyHats(count:int):void
  {
    var originalHatCount:int = this._catSimulation.hatCount - count; //The number of hats before this most recent addition.
    var countForCats:int = int(Math.min(count, this._catSimulation.catCount - originalHatCount)); //Number of hats for cats
    var surplus:int = count - countForCats; //Surplus hats beyond the cats.

    var catUpperBound:int = originalHatCount + countForCats; //When to stop adding to cats.
    for (var index:int = originalHatCount; index < catUpperBound; index++)
    {
      this._actionQueue.add(new BuyClothingAction(this._cats[index], new _hatClass(), 0.25));
    }

    //Render surplus!
    if (surplus > 0)
    {
      this._actionQueue.add(new BuySurplusClothingAction(this._surplusHatDisplay, this._catSimulation.hatCount - this._catSimulation.catCount));
    }
  }

  public function buyMittens(count:int):void
  {
    var originalMittenCount:int = this._catSimulation.mittenCount - count; //The number of mittens before this most recent addition.
    var countForCats:int = int(Math.min(count, (this._catSimulation.catCount * 4) - originalMittenCount)); //Number of hats for cats
    var surplus:int = count - countForCats; //Surplus hats beyond the cats.

    for (var index:int = originalMittenCount;
         index < originalMittenCount + countForCats;
         index++)
    {
      var catIndex:int = int(index / 4);
      var mittenOffset:int = index % 4;
      var mittenDisplayObject:DisplayObject;
      switch (mittenOffset)
      {
        case 0:
          mittenDisplayObject = new _mitten0Class();
          break;
        case 1:
          mittenDisplayObject = new _mitten1Class();
          break;
        case 2:
          mittenDisplayObject = new _mitten2Class();
          break;
        case 3:
          mittenDisplayObject = new _mitten3Class();
          break;
      }
      this._actionQueue.add(new BuyClothingAction(this._cats[catIndex], mittenDisplayObject, 0.1));
    }

    //Render surplus!
    if (surplus > 0)
    {
      this._actionQueue.add(new BuySurplusClothingAction(this._surplusMittenDisplay, this._catSimulation.mittenCount - (this._catSimulation.catCount * 4)));
    }
  }
}
}